package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.userquery.parameter.GetFirstOrderStatusBean;
import com.ymatou.iapi.trading.userquery.service.GetFirstOrderStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;


/**
 * 获取用户首单信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetFirstOrderStatus {
	private static GetFirstOrderStatusBean getfirstorderstatusBean;
	private static GetFirstOrderStatusCall getfirstorderstatusCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户首单信息接口");
	}

	@Before
	public void caseUp() {
		getfirstorderstatusBean = new GetFirstOrderStatusBean();
		getfirstorderstatusCall = new GetFirstOrderStatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getfirstorderstatusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetFirstOrderStatus_001() {
		Logger.start(true, "只下单，没有首单信息，获取用户首单信息成功");
		try {
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			TradingPlaceOrderCallService.placeOrderWithUserId(userId);

			List<Integer> buyerIds = new ArrayList<Integer>();

			buyerIds.add(userId);

			getfirstorderstatusBean.setBuyerIds(buyerIds);

			getfirstorderstatusCall.setData(getfirstorderstatusBean);
			getfirstorderstatusCall.callService();

			boolean actual = getfirstorderstatusCall.getData()
					.getJSONObject("buyerOrders")
					.optBoolean(String.valueOf(25396423));

			Logger.verifyEquals(false, actual, "验证buyerOrders");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetFirstOrderStatus_002() {
		Logger.start(true, "确认收货后,获取用户首单信息成功");
		try {
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			TradingCallService.confirmReceive(userId, orderId);

			TradingSyncService.getSyncResult(orderId);

			List<Integer> buyerIds = new ArrayList<Integer>();

			buyerIds.add(userId);

			getfirstorderstatusBean.setBuyerIds(buyerIds);

			getfirstorderstatusCall.setData(getfirstorderstatusBean);
			getfirstorderstatusCall.callService();
			
			boolean actual = getfirstorderstatusCall.getData()
					.getJSONObject("buyerOrders")
					.getBoolean(String.valueOf(userId));

			Logger.verifyEquals(true, actual, "验证buyerOrders");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}